import mx.utils.Delegate;

import org.molamil.acto.mc.McConf;
import org.molamil.acto.mc.mcw.LcMcWrapper;

/**
 * <code>LcMcWrapper</code> with a ping-pong representation of IN and OUT.
 * When the <code>state</code> is set to IN, the MC plays until the end of the timeline, 
 * ignoring stops.
 * When the <code>state</code> is set to OUT, the MC plays backwards until the beginning 
 * of the timeline, ignoring stops.
 *
 * @author Jorge Hernandez
 */
class org.molamil.acto.mc.mcw.PingPong extends LcMcWrapper {
	
	
	// Properties
	
	private var monitor:MovieClip;
	
	private var onEfInDelegate:Function;
	private var onEfOutDelegate:Function;
	
	
	// Constructor
	
	public function PingPong() {
		super();
		onEfInDelegate = Delegate.create(this, onEfIn);
		onEfOutDelegate = Delegate.create(this, onEfOut);
	}
	
	
	// Functions
	
	private function changeState():Void {
		
		// super.changeState() will dispatch the events.
		super.changeState();
		
		// Check the state.
		if (state == STATE_IN || state == STATE_OUT) {
			
			// Create a monitor mc.
			if (monitor == null)
				monitor = mc.createEmptyMovieClip(McConf.monitorMcBaseName, mc.getNextHighestDepth());
			
			// Assign the onEnterFrame.
			monitor.onEnterFrame = state == STATE_IN ? onEfInDelegate : onEfOutDelegate;
			
		}
		
	}
	
	private function onEfIn():Void {
		mc.nextFrame();
		if (mc._currentframe == mc._totalframes) {
			state = STATE_ON;
			monitor.removeMovieClip();
			monitor = null;
		}
	}
	
	private function onEfOut():Void {
		mc.prevFrame();
		if (mc._currentframe == 1) {
			state = STATE_OFF;
			monitor.removeMovieClip();
			monitor = null;
			mc.stop();
		}
	}
	
	
}